package com.easyOrder.core.dynamics.config;

import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;


/**
 * Created by yanbin on 2016/12/19.
 * 动态配置
 */
@Component
public class ConfigComponent {

    private static Map<String, Config> configs = Maps.newHashMap();

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private boolean init;

    public ConfigComponent() {

    }

    private void initData() {
        if (!init) {
            ConfigTypes[] types = ConfigTypes.values();
            for (ConfigTypes ctype : types) {
                String key = "config_" + ctype.toString();
                String value = stringRedisTemplate.opsForValue().get(key);
                if (!StringUtils.isBlank(value)) {
                    Config config = new Config();
                    config.setKey(key);
                    config.setValue(value);
                    config.setLastUpdate(System.currentTimeMillis());
                    configs.put(key, config);
                }
            }
            init = true;
        }
    }

    public String getValue(ConfigTypes type, String defaultValue) {
        initData();
        String key = "config_" + type.toString();
        if (!isExists(key)) {
            set(type, defaultValue);
            return defaultValue;
        } else {
            return getValue(key);
        }
    }

    public void set(ConfigTypes type, String value) {
        String key = "config_" + type.toString();
        Config config = new Config();
        config.setKey(key);
        config.setValue(value);
        config.setLastUpdate(System.currentTimeMillis());
        configs.put(key, config);
        stringRedisTemplate.opsForValue().set(key, value);
    }

    private String getValue(String key) {
        if (canGetConfig(key))
            return getConfigValue2(key);
        else
            return getConfigValue(key);
    }

    private boolean isExists(String key) {
        return configs.containsKey(key);
    }

    private boolean canGetConfig(String key) {
        Long end = System.currentTimeMillis();
        Config config = configs.get(key);
        Long timeSpan = (long) 2 * 60 * 1000;
        return (end - config.getLastUpdate()) > timeSpan;
    }

    private String getConfigValue(String key) {
        Config config = configs.get(key);
        if (config != null)
            return config.getValue();
        else
            return null;
    }

    private String getConfigValue2(String key) {
        String result = stringRedisTemplate.opsForValue().get(key);
        Config config = configs.get(key);
        if (StringUtils.isBlank(result)) {
            if (config != null) {
                return config.getValue();
            } else {
                return null;
            }
        } else {
            if (config != null) {
                config.setLastUpdate(System.currentTimeMillis());
                config.setKey(key);
                config.setValue(result);
            }
            return result;
        }
    }
}
